import useClipboard from 'vue-clipboard3'; //复制
const { toClipboard } = useClipboard();
//导出数据
import * as XLSX from 'xlsx';
import * as XLSX_STYLE from 'xlsx-style-vite';
//number丢失精度
import * as $math from 'mathjs';
import moment from 'moment'; // 导入时间过滤器
//获取路由
const com: any = {
  //储存缓存
  setItem(keyName: string, value: any) {
    if (typeof value === 'string') {
      return localStorage.setItem(keyName, value);
    } else {
      return localStorage.setItem(keyName, JSON.stringify(value));
    }
  },
  //获取缓存
  getItem(keyName: string) {
    let _value: any = localStorage.getItem(keyName);
    if (this.needsJSONParse(_value)) {
      return JSON.parse(_value);
    } else {
      return _value || '';
    }
  },
  //清除缓存
  removeItem(keyName: string) {
    localStorage.removeItem(keyName);
  },
  //判断是否符合JSONParse
  needsJSONParse(data: any) {
    if (typeof data === 'string') {
      try {
        // 尝试解析 JSON 字符串
        const parsedData = JSON.parse(data);
        return true;
      } catch (e) {
        // 如果解析失败，说明这不是一个有效的 JSON 字符串
        return false;
      }
    }
    // 如果数据不是字符串，则不需要解析
    return false;
  },

  //提示
  message(name: any = '操作成功', type: any = 'success', showClose: any = false) {
    ElMessage({
      showClose: showClose,
      message: name,
      type: type,
    });
  },
  messageBox(message: any = '操作成功', fun: Function, type?: string) {
    ElMessageBox.confirm(message, '消息提示', {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: type,
    })
      .then(() => {
        fun();
      })
      .catch(() => {});
  },
  // 文本复制
  copyText(text: string, isState: boolean = true, message: string = '复制成功') {
    if (text) {
      // 获取要复制的文本
      let textToCopy = text;
      // 创建一个隐藏的textarea元素
      let textarea = document.createElement('textarea');
      // 将文本设置为textarea的值
      textarea.value = textToCopy;
      // 将textarea添加到页面中
      document.body.appendChild(textarea);
      // 选中textarea中的文本
      textarea.select();
      // 复制选中的文本到剪贴板
      document.execCommand('copy');
      // 从页面中移除textarea元素
      document.body.removeChild(textarea);
      if (isState) {
        ElMessage({
          showClose: false,
          message: message,
          type: 'success',
        });
      }
    }
  },
  //打开弹窗
  openPup(pup: any, typeName?: any) {
    pup.state = true;
    pup.typeName = typeName;
  },
  //模拟点击空白页面
  clickEvent() {
    var clickEvent = new MouseEvent('click', {
      view: window,
      bubbles: true,
      cancelable: true,
    });

    // 获取空白区域的元素
    let blankArea: any = document.getElementById('app');
    // 模拟点击空白区域
    blankArea.dispatchEvent(clickEvent);
  },

  //计算
  count(t1: any = 0, t2: any = 0, formula: string = '*', number?: number) {
    let num = 0;
    if ((t1 || t1 === 0) && (t2 || t2 === 0)) {
      switch (formula) {
        case '+': //加法
          num = parseFloat(
            $math.format(
              $math
                .chain(Number(t1) || 0)
                .add($math.bignumber(Number(t2) || 0))
                .done(),
            ),
          );
          break;
        case '-': //减法
          num = parseFloat(
            $math.format(
              $math
                .chain(Number(t1) || 0)
                .subtract($math.bignumber(Number(t2) || 0))
                .done(),
            ),
          );
          break;
        case '*': //乘法
          num = parseFloat(
            $math.format(
              $math
                .chain(Number(t1) || 0)
                .multiply($math.bignumber(Number(t2) || 0))
                .done(),
            ),
          );
          break;
        case '/': //除法
          num = parseFloat(
            $math.format(
              $math
                .chain(Number(t1) || 0)
                .divide($math.bignumber(Number(t2) || 1))
                .done(),
            ),
          );
          break;
      }
    }
    let _num = number ? num.toFixed(number) : num;
    _num = Number(_num);
    return _num;
  },
  //设置数字
  getNum(num?: number | string, unit?: string) {
    let _num: number = 8;
    if (typeof num === 'number') {
      _num = num;
    } else if (typeof num === 'string') {
      // 更严格的匹配规则，确保只匹配有效的浮点数
      const floatRegex = /-?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][-+]?\d+)?/g;
      const matches = num.match(floatRegex);
      if (matches) {
        _num = matches.map(Number)[0];
      }
    }
    if (unit) {
      return _num + unit;
    }
    return _num;
  },
};
export default com;
